package com.jm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jm.common.exception.ServiceException;
import com.jm.common.util.entity.SelectCondition;
import com.jm.dao.ToyMapper;
import com.jm.pojo.Category;
import com.jm.pojo.Toy;
import com.jm.pojo.dto.ToyDto;
import com.jm.service.CategoryService;
import com.jm.service.CommonService;
import com.jm.service.ToyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ToyServiceImpl extends ServiceImpl<ToyMapper, Toy> implements ToyService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CommonService commonService;

    @Override
    @Transactional
    public boolean removeBatchByIds(Collection<?> list) {
        for (Object o : list) {
            String id = String.valueOf(o);
            Toy toy = super.getById(id);
            boolean removeFileResult = commonService.delFile(toy.getImage());
            if (!removeFileResult) throw new ServiceException("删除文件失败");
        }
        boolean removeBatchByIds = super.removeBatchByIds(list);
        if (!removeBatchByIds) throw new ServiceException("批量删除失败");
        return true;
    }

    /**
     * 分页条件查询
     */
    @Override
    public IPage<Toy> selectByPageCondition(SelectCondition condition) {
        Long currentPage = condition.getPage();
        Long pageSize = condition.getPageSize();
        if (currentPage == null || pageSize == null)
            throw new ServiceException("分页条件出现错误");
//        构造分页
        IPage<Toy> page = new Page<>(currentPage, pageSize);
//        构造查询条件
        LambdaQueryWrapper<Toy> qw = new LambdaQueryWrapper<>();
        qw.like(condition.getName() != null, Toy::getName, condition.getName());
//        进行查询
        page(page, qw);
        return page;
    }

    /**
     * 将单品对象转换为单品对象的扩展对象
     */
    @Override
    public ToyDto toDto(Toy toy) {
        ToyDto dto = new ToyDto();
        Long categoryId = toy.getCategoryId();
        Category category = categoryService.getById(categoryId);
        BeanUtils.copyProperties(toy, dto);
        dto.setCategoryName(category.getName());
        return dto;
    }

    /**
     * 将分页后的单品对象转换为分页后的扩展对象
     */
    @Override
    public IPage<ToyDto> toDto(IPage<Toy> toyIpage) {
        List<ToyDto> collect = toyIpage.getRecords().stream().map(new Function<Toy, ToyDto>() {
            @Override
            public ToyDto apply(Toy toy) {
                return toDto(toy);
            }
        }).collect(Collectors.toList());
        IPage<ToyDto> dto = new Page<>();
        BeanUtils.copyProperties(toyIpage, dto, "records");
        dto.setRecords(collect);
        return dto;
    }

    /**
     * 批量修改状态
     */
    @Override
    public boolean changeStatusByIds(List<String> ids, Integer status) {
        List<Toy> collect = new ArrayList<>();
        if (ids != null) {
            collect = ids.stream().map(s -> {
                        Toy toy = new Toy(Long.parseLong(s), status);
                        return toy;
                    }
            ).collect(Collectors.toList());
        }
        return updateBatchById(collect);
    }

    @Override
    public List<Toy> getByCategoryId(String categoryId) {
        LambdaQueryWrapper<Toy> qw = new LambdaQueryWrapper<>();
        qw.eq(true, Toy::getCategoryId, categoryId);
        return list(qw);
    }

}
